<?php

#################################################################
#								HTML
#################################################################

/**
 * From component registed as 'element/form' 
 * 
 * $vars['action']
 * $vars['method']
 * $vars['name']
 * $vars['id']
 * $vars['class']
 * $vars['fieldset_class']
 * $vars['file_upload']
 * 
 * $vars['content']
 * 
 * $vars['submit_callback']
 */

if($_CONTEXT=='HTML'){

	$_CSS_STACK->push(blg_get_site_url().'element/form.css', TRUE);
	$_JS_STACK->push(blg_get_site_url().'element/form.js', TRUE);

	//PREPARE DEFAULTS
	if(empty($vars['action']))
		$vars['action']= '#';
		
	if(empty($vars['method']))
		$vars['method']= 'POST';		
		
	if(empty($vars['id']))
		$vars['id']= 'form_'.random_string(12);
	
	if(empty($vars['fieldset_class']))
		$vars['fieldset_class']= 'blg_fieldset';
		
	if(!is_bool($vars['file_upload']))
		$vars['file_upload']= FALSE;

	$class= 'blg_form';
	
	//BUILD
	$form = '<form id="'.$vars['id'].'" blg_id="'.$vars['id'].'"';
	
	if(!empty($vars['name']))
		$form.= ' name="'.$vars['name'].'"';

	if($vars['file_upload'])
		$form.= ' enctype="multipart/form-data"';
	
	if(!empty($vars['class']))
		$form.= ' class="'.$class.' '.$vars['id'].'"';
	else
		$form.= ' class="'.$class.'"';
		
	$form.= ' action="'.$vars['action'].'" method="'.$vars['method'].'"';
		
	$form.= '>'."\n";
	
	if($vars['file_upload'])
		$form.= '<input type="hidden" name="MAX_FILE_SIZE" value="'.blg_get_config('upload_max_filesize').'">'."\n";
	
	if(!empty($vars['content'])){
		if(is_string($vars['content'])){
			$form.= $vars['content'];
		}
		elseif(is_array($vars['content'])){
			foreach($vars['content'] as $fieldset){
				if(is_string($fieldset))
					$form.= $fieldset;
				elseif(is_array($fieldset)){
					if(count($fieldset)==1){
						$form.= $fieldset[0];
					}					
					elseif(count($fieldset)==2){
						$form.= '
						<fieldset class="'.$vars['fieldset_class'].'">
							<legend>'.$fieldset[0].'</legend>
							'.$fieldset[1].'
						</fieldset>
						';
					}
				}				
			}			
		}
	}
	
	$form.= '</form>'."\n";
	
	echo $form;	

	if(!empty($vars['submit_callback'])){
		$code="
			$('#".$vars['id']."').submit(function(){
				".add_trainling_slash($vars['submit_callback'], ';')."
			});
		";
		
		$_JS_CODE->append($code, ON_DOC_READY);
	}
	
}
#################################################################
#								JS
#################################################################
elseif($_CONTEXT=='JS'){ ?>

<?php }
#################################################################
#								CSS
#################################################################
elseif($_CONTEXT=='CSS'){ ?>

form.blg_form fieldset.blg_fieldset {
	padding: 5px 6px 8px 6px;
	margin: 0 0 15px 0;
	
	border: 1px solid <?php echo blg_color('blue', 'light');?>;
	border-radius: 4px;
}

form.blg_form fieldset.blg_fieldset legend {
	padding: 0px 10px;
	margin: 0 10px;
	border-top: 1px solid <?php echo blg_color('blue', 'light');?>;
	border-left: 1px dotted <?php echo blg_color('blue', 'light');?>;
	border-right: 1px dotted <?php echo blg_color('blue', 'light');?>;
	color: <?php echo blg_color('blue', 'light');?>;
	font-size: 12px;
	font-weight: bold;
	letter-spacing: 1px;
}	

<?php }
